package com.xywy.find.paser;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.find.bean.JkgcArticle;

/**
 * Created by xiaogd on 8/4/14. 健康广场 -内容详情页 json解析类 (1) 获取 文章 (2) 赞 (3) 收藏
 * 
 * 
 */
public class JkgcArticlePaser {
	public static final int ARTICLE = 1; // (1) 获取 文章
	public static final int GOOD = 2; // (2) 赞
	public static final int COLLECT = 3; // (3) 收藏

	public static final int FAILED = 4; // 失败
	public static final int FIND = 5; // 查找
	public static final int HELPED = 6; // 帮助

	private static final String TAG = "JkzhArticlePaser";
	private Context mContext;
	private Handler mHandler;

	private int what;

	public int getWhat() {
		return what;
	}

	public void setWhat(int what) {
		this.what = what;
	}

	public JkgcArticlePaser(Context context, Handler handler, int what) {
		this.mContext = context;
		this.mHandler = handler;
		this.what = what;
	}

	// 封装Parser参数
	private Parser paramsParser;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求
	public void sendMsg(String action, HashMap<String, Object> inParams,
			String request) {
		paramsParser = new Parser();
		// 请求路径
		paramsParser.action = action;
		// 请求方式
		paramsParser.request = request;
		// 参数
		paramsParser.map = inParams;
		// 传递上下文
		paramsParser.context = this.mContext;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.what = what;
			Gson gson = new Gson();
			JkgcArticle data = null;
			try {
				if (resultParser.back == null) {
					message.what = FAILED;
					mHandler.sendMessage(message);
					return;
				}
				switch (what) {
				case COLLECT:
					JSONObject jsonCollect = new JSONObject(resultParser.back);
					int result = jsonCollect.optInt("state");
					if (result == 200) {
						message.obj = "成功";
					} else {
						message.obj = jsonCollect.optString("error");
					}
					break;
				case ARTICLE:
					JSONObject jsonObject = new JSONObject(resultParser.back);
					data = gson.fromJson(jsonObject.getString("data"),
							JkgcArticle.class);
					message.obj = data;
					break;
				case GOOD:
					break;
				case FIND:
					message.obj = resultParser.back;
					break;

				default:
					break;
				}

			} catch (JSONException e) {
				e.printStackTrace();
				data = null;
				message.obj = data;
			} finally {
				mHandler.sendMessage(message);
			}

		}

		public <T extends Parser> void onFailure(T parserResult, int resultCode) {
			mHandler.sendEmptyMessage(FAILED);
		};
	};

	/**
	 * 封装返回的json
	 */
	// private JkgcArticle getJkgcArticle(String jsonStr) {
	//
	// JkgcArticle article = new JkgcArticle();
	//
	// try {
	// JSONObject jsonObject = new JSONObject(jsonStr);
	// article.setDetail(jsonObject.optString("detail"));
	// article.setTitle(jsonObject.optString("title"));
	// article.setGood(jsonObject.optInt("good"));
	// article.setContentid(jsonObject.optLong("contentid"));
	// article.setPubdate(jsonObject.optString("pubdate"));
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return article;
	// }
}
